require(['Jquery','Hui','HuiAdmin','Layer','Icheck','Datepicker','DatepickerCN'], function(jquery,hui,huiadmin,layer,iCheck){

	// 添加
	admin_add = function(){
		$('.add-btn').click(function() {

			var html = $('#admin-new-page').html();
			var url = $(this).attr('data-url');
			layer.open({
			    type: 1,
			    title: '添加后台用户',
			    shade: 0.5,
			    scrollbar : false,
			    area : ['60%','60%'],
			    content: html,
			    btn: ['确认', '取消'],
			    yes: function(index, frame){
				    var self = $(this),
				    name = $(frame).find("#admin-name").val(),
				    pas1 = $(frame).find("#password1").val(),
				    pas2 = $(frame).find("#password2").val(),
				    form = $(frame).find("#add-user-form");

				    if (!name) {
				      	layer.alert('帐号不能为空',{icon: 2});
				      	return;
				    }
				    if (!pas1) {
				      	layer.alert('密码不能为空',{icon: 2});
				      	return;
				    }
				    if (!pas2) {
				      	layer.alert('确认密码不能为空',{icon: 2});
				      	return;
				    }
				    if (pas1 != pas2) {
				      	layer.alert('两次输入的密码不一致',{icon: 2});
				      	return;
				    }

			      	var formData = form.serialize();

			      	$.post(url,formData,function(json) {
			      		if (json.code) {
			      			layer.alert(json.msg,{icon: 1});
			      			pageReload(1500);
			      		} else {
			      			layer.alert(json.msg,{icon: 5});//图标5是为了区分js的报错
			      		}

			      	},"json");
			  	},
			});
		});
	}

	// 编辑
	admin_edit = function(){
		$('.edit-btn').click(function() {
			var self = $(this),
			url = self.attr("data-url");

			layer.open({
			  type: 2,
			  title: '编辑用户信息',
			  shade: 0.5,
			  scrollbar : false,
			  area : ['65%','65%'],
			  content: url,
			  cancel: function(){ 
			      //右上角关闭回调
			      pageReload(500);
			    }
			}); 

		});
	}

	// 用户详情
	admin_details = function(){
		$('.details-btn').click(function() {
			var self = $(this),
			url = self.attr("data-url");

			layer.open({
			  type: 2,
			  title: '用户详情',
			  shade: 0.5,
			  scrollbar : false,
			  area : ['80%','80%'],
			  content: url,
			  cancel: function(){ 
			      //右上角关闭回调
			      pageReload(500);
			    }
			}); 

		});
	}

	// 删除
	admin_del = function() {
		$('.delete-btn').click(function(){
			var self = $(this),
				id   = self.attr('data-id'),
				url  = self.attr('data-url');
			layer.msg('确定要删除吗？', {
				time: 0 ,//不自动关闭,
				btn: ['确定', '关闭'],
				yes: function(index){
				    layer.close(index);
						var params = {id: id};
						$.post(url, params, function(json){
							if (json.code) {
								layer.alert(json.msg,{icon: 1});
								pageReload(1000);
							} else {
								layer.alert(json.msg,{icon: 2});
							}
						}, 'json');
				}
			});
		});
	};

	// 批量处理
	batch_proc = function(){
		$('.batch-btn').click(function(){
			var self = $(this),
				url  = self.attr('data-url'),
				type  = self.attr('data-type'),
				title  = self.attr('data-title');
			var ids = get_checkbox_group_value('user_id[]');

			if (ids == false) {
				layer.alert('请选择要批量'+ title +'的用户',{icon: 2});
				return false;
			}
// alert(ids);
			layer.alert('确定要批量'+ title +'选中用户吗？', {
				time: 0 ,//不自动关闭,
				btn: ['确定', '关闭'],
				yes: function(index){
				    layer.close(index);
						var data = {ids: ids, type:type};
						$.post(url, data, function(json){
							if (json.code) {
								layer.alert(json.msg,{icon: 1});
								pageReload(1000);
							} else {
								layer.alert(json.msg,{icon: 5});
							}
						}, 'json');
				}
			});
		})
	}

	// 单选框复选框样式事件
	iCheckStyle('select_all','user_id[]');

	//日期选择
	datepicker('#search-form','.datepicker-style');

	// 添加
	admin_add();

	// 编辑
	admin_edit();

	// 删除
	admin_del();

	// 用户详情
	admin_details();

	// 批量处理
	batch_proc();

});